package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountEvent;
import com.tbit.main.service.AccountEventService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 大事件
 *
 * @author LMY
 * @create 2020-11-12 14:33
 */
@RestController
@RequestMapping("/accountEvent")
public class AccountEventController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountEventService accountEventService;

    public static void main(String[] args) {
        String a = "11923,11933,11926,12002,500269,500183,500262,11937,500290,500185,500265,500303,500193,11927,500266,12003,500316,500216,11928,500267,500182,500220";
        Stream.of(a.split(",")).forEach(x -> System.out.println(x));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public JsonResult add(String token, AccountEvent accountEvent, Integer mapType) {

        /**检验*/
        Assert.notNull(accountEvent.getTitle(), "AccountEvent.title.notNull");
        Assert.notNull(accountEvent.getContent(), "AccountEvent.content.notNul");
        Assert.notNull(accountEvent.getLevel(), "AccountEvent.level.notNull");
        Assert.notNull(accountEvent.getStartTime(), "System.startTime.notNull");
        Assert.notNull(accountEvent.getAccountId(), "AccountUser.agentId.notNull");

        /** 备注 */
        if (accountEvent.getRemark() == null) {
            accountEvent.setRemark("");
        }
        accountEvent.setAddTime(DateTimeUtil.getNowTime());

        accountEventService.insert(accountEvent);

        return JsonResult.succ();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public JsonResult update(String token, AccountEvent accountEvent, Integer mapType) {

        /**检验*/
        Assert.notNull(accountEvent.getTitle(), "AccountEvent.title.notNull");
        Assert.notNull(accountEvent.getContent(), "AccountEvent.content.notNul");
        Assert.notNull(accountEvent.getLevel(), "AccountEvent.level.notNull");
        Assert.notNull(accountEvent.getStartTime(), "System.startTime.notNull");
        Assert.notNull(accountEvent.getAccountId(), "AccountUser.agentId.notNull");

        /** 备注 */
        if (accountEvent.getRemark() == null) {
            accountEvent.setRemark("");
        }

        /**查询大事件*/
        AccountEvent accountEventDB = accountEventService.byAccountEventId(accountEvent.getAccountId());

        /**替换可修改字段*/
        ObjectUtil.assign(accountEvent, accountEventDB, null);
        accountEvent.setAddTime(DateTimeUtil.getNowTime());

        accountEventService.update(accountEvent);

        return JsonResult.succ();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public JsonResult delete(String token, Integer accountEventId) {
        /* 校验 */
        Assert.notNull(accountEventId, "AccountEvent.control.accountEventId.notNull");

        accountEventService.del(accountEventId);
        return JsonResult.succ();
    }

    /**
     * 根据时间查询(APP)
     */
    @RequestMapping("/getEventByAccountId")
    public JsonResult getEventByAccountId(Integer accountId, String startTime, String endTime) {
        Assert.notNull(accountId,  "AccountUser.agentId.notNull");

        List<AccountEvent> accountEvents=accountEventService.ByTime(accountId,startTime,endTime);

        return JsonResult.succ(accountEvents);
    }

    /**
     * 根据时间查询(H5)
     */
    @RequestMapping("/getEventByAccountIds")
    public JsonResult getEventByAccountIds(String accountIds, String startTime, String endTime) {
        if (StringUtils.isBlank(accountIds)) {
            throw new BaseException("AccountUser.agentId.notNull");
        }
        List<List<AccountEvent>> accountEvents = Stream.of(accountIds.split(","))
                .map(x -> accountEventService.ByTime(Integer.parseInt(x), startTime, endTime))
                .collect(Collectors.toList());
        return JsonResult.succ(accountEvents);
    }
}
